﻿#include "FITKAcousticsHDF5IOInterface.h"
#include "FITKAcousticsHDF5Reader.h"
#include "FITKAcousticsHDF5Writer.h"
#include <QThread>

namespace IO
{
    bool FITKAcousticsHDF5IOInterface::exec(const int indexPort)
    {
        //如果正在执行，则退出，不能同时执行多个任务
        _success = false;
        if (this->isRunning()) return false;

        switch (indexPort)
        {
        case 1:  //读取
            _io = new FITKAcousticsHDF5Reader(this, _success);
            break;
        case 2:  //写出
            _io = new FITKAcousticsHDF5Wriiter(this, _success);
            break;
        default: break;
        }
        //参数错误，直接退出
        if (_io == nullptr) return false;
        _io->setFileName(_fileName);
        //在线程中执行
        if (_runInThread)
        {
            connect(_io.getObjectPtr(), SIGNAL(taskFinishedSig(FITKThreadTask*)), this, SIGNAL(ioThreadFinishedSig()));
            _io->push2ThreadPool();
            QThread::msleep(100);
        }
        //不在线程执行
        else
        {
            _io->run();
            auto p = _io.getObjectPtr();
            if (p) delete p;
        }

        //参数传递完成，清空参数
        this->resetDataObjects();
        return _success;
    }
    void FITKAcousticsHDF5IOInterface::setFileName(const QString& fileName)
    {
        _fileName = fileName;

    }

    void FITKAcousticsHDF5IOInterface::runInThread(const bool & inThread /*= true*/)
    {
        _runInThread = inThread;
    }

    bool FITKAcousticsHDF5IOInterface::isRunning()
    {
        return _io != nullptr;
    }

    bool FITKAcousticsHDF5IOInterface::isSuccess()
    {
        return _success;
    }

    QString FITKAcousticsHDF5IOInterface::getComponentName()
    {
        return "AcousticsHDF5IO";
    }

}
